import Vue from 'vue'
import reshandler from 'services/response-handler'
import femal from "assets/femal.png";
import male from "assets/male.png";
import icon_male from "assets/icon_male.png";
import icon_female from "assets/icon_female.png";
/*
 * 用户信息
 */
function getTime() {
    var d = new Date();
    var yy = d.getFullYear();
    var mm = d.getMonth() + 1;
    var dd = d.getDate();
    var hh = d.getHours();
    var MM = d.getMinutes();
    var ss = d.getSeconds();
    var time = yy + "-" + (mm > 9 ? mm : '0' + mm) + "-" + (dd > 9 ? dd : '0' + dd) + " " + hh + ":" + MM + ":" + ss;
    return time
}
function makeData(res) {
    console.log(res);
    let head;
    let genderIcon
    let hasHead = false
    let nowTime = getTime();
    if (res.member.head === undefined) {
        if (res.member.gender === 0) {
            head = male
        } else if (res.member.gender === 1) {
            head = female
        } else {
            head = femal
        }
    }else{
        hasHead = true ;
        head =  res.member.head
    }
    if (res.member.gender === '0') {
        genderIcon = icon_male
    } else if (res.member.gender === '1') {
        genderIcon = icon_female
    } else {
        genderIcon = icon_male
    }
    for (var i = 0; i < res.postList.length; i++) {
        res.postList[i].avatar = res.member.head;
        res.postList[i].avatar = res.member.head;
        res.postList[i].isShowTime = true;
        if (res.postList[i].avatar === undefined) {
            res.postList[i].avatar = head
        }
        res.postList[i].userId = Vue.prototype.$.userId;
        let publishDate = res.postList[i].publishDate
        if (publishDate.substring(0, 10) === nowTime.substring(0, 10)) {
            res.postList[i].showTime = '今天'
        } else if (publishDate.substring(0, 4) !== nowTime.substring(0, 4)) {
            res.postList[i].showTime = publishDate.substring(0, 10)
        } else {
            res.postList[i].showTime = publishDate.substring(5, 10)
        }
    }
    console.log(res);
    return {
        spaces: res.postList,
        headUrl: head,
        gender: res.member.gender,
        userName: res.member.nickname,
        genderIcon:genderIcon,
        hasHead: hasHead
    }
}

export default function (params, success, fail) {
    this.$.get(params).then(reshandler.success(success, fail, makeData), reshandler.error(fail))
}
